# TrayIcon.py
# Copyright (C) otacon 2010 <orfeo18@gmail.com>
# 
# AdSense-Monitor is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# AdSense-Monitor is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk
import gobject
import os

from core import Path
from core.ReportMonitor import ReportMonitor
from core.Report import *

from gui.TrayMenu import TrayMenu
from gui.GtkTrayIcon import GtkTrayIcon
try:
    from gui.Win32TrayIcon import Win32TrayIcon
    windows = True
except:
    windows = False

class TrayIcon(gobject.GObject):

    if windows:
        OK_ICON = os.path.join(Path.DATA_PATH,"ok.ico")
        KO_ICON = os.path.join(Path.DATA_PATH,"ko.ico")
        REFRESH_ICON = os.path.join(Path.DATA_PATH,"refresh.ico")
    else:
        OK_ICON = os.path.join(Path.DATA_PATH,"ok.png")
        KO_ICON = os.path.join(Path.DATA_PATH,"ko.png")
        REFRESH_ICON = os.path.join(Path.DATA_PATH,"refresh.png")

    def __init__(self , gtk_window=None):
        gobject.GObject.__init__(self)

        if windows:
            if gtk_window:
                self.tray = Win32TrayIcon(TrayIcon.KO_ICON, self, gtk_window)
            else:
                self.tray = GtkTrayIcon.GtkTrayIcon(TrayIcon.KO_ICON, self)
                
        else:
            self.tray = GtkTrayIcon(TrayIcon.KO_ICON, self)

    def set_icon(self,icon):
        self.tray.set_icon(icon)

    def set_tooltip(self,text):
        self.tray.set_tooltip(text)

    def set_menu(self,menu):
        self.tray.set_menu(menu)

    def show_notify(self, text):
        if os.name == 'nt':
            self.tray.show_notify(text)

    def on_status_changed(self,status):
        if status == ReportMonitor.STATUS_OK:
            self.set_icon(TrayIcon.OK_ICON)
        elif status == ReportMonitor.STATUS_UPDATE:
            self.set_icon(TrayIcon.REFRESH_ICON)
            self.set_tooltip('Updating...')
        else:
            self.set_icon(TrayIcon.KO_ICON)
            self.set_tooltip('Failed to connect to Google.com')
        return

    def remove(self):
        self.tray.remove()
